#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string compressString(string S) {
        string ret;
        int n = 0;
        char c = ' ';

        for (auto ch : S)
        {
            if (c == ch)
            {
                n++;
            }
            else
            {
                if (c != ' ')
                {
                    ret += c;
                    char cr[6];
                    sprintf(cr, "%d", n);
                    ret += cr;
                }
                c = ch;
                n = 1;
            }
        }
        ret += c;
        char cr[6];
        sprintf(cr, "%d", n);
        ret += cr;
        if (ret.size() < S.size())
            return ret;
        else
            return S;
    }
};